<?php

namespace Modules\Admin\Http\Controllers\Configurations;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Modules\Common\General\Security\Api\Gateway;
use Modules\Common\Models\AdminLog;
use Modules\Common\Models\Configurations;
use Modules\Common\Models\Helpers\TrashManager;
use Modules\Common\Util\Api;
use Modules\Common\Util\Database;
use Modules\Common\Exceptions\Base\Exception;


class Delete
{
    /**
     * @Route(path=/api/admin/configuration  method=delete)
     */
    public function index()
    {

        DB::transaction(function () {

            $configuration = Configurations::find(request('id'));
            if ($configuration == null) {
                Exception::new()
                    ->tellUser('配置项不存在')
                    ->logNotice('配置项不存在', ['request' => request()->all()])
                    ->throw();
            }
            TrashManager::startTrace(sprintf('删除配置项：%s', $configuration->name));

            // 删除操作
            $configuration->delete();

            TrashManager::endTrace();
        });


        return Api::success();
    }
    
    
    /**
     * @Route(path=/api/admin/configuration-batch-delete  method=delete)
     */
    public function index2()
    {
        // 假设请求中传递了一个包含多个分类 ID 的数组，例如：ids=1,2,3
        $configurationIds = request('ids');
        
        // 将字符串转换为数组
        $configurationIdsArray = explode(',', $categoryIds);
    
        // 启动数据库事务
        DB::transaction(function () use ($configurationIdsArray) {
            foreach ($configurationIdsArray as $configurationId) {
                $configuration = Categories::find($configurationId);
                if ($configuration == null) {
                    Exception::new()
                        ->tellUser('配置项不存在')
                        ->logNotice('配置项不存在', ['request' => request()->all()])
                        ->throw();
                }
                // 开始记录删除操作，添加至回收站
                TrashManager::startTrace(sprintf('删除配置项：%s', $configuration->name));
    
                // 删除操作
                $configuration->delete();
    
                TrashManager::endTrace();
            }
        });
         return Api::success();
    }

}